package com.pengtu.gsj.service.impl.system;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.system.OrganizationDao;
import com.pengtu.gsj.entity.app.Organization;
import com.pengtu.gsj.service.system.OrganizationService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.encode.JsonBinder;

@Transactional
@Service
public class OrganizationServiceImpl implements OrganizationService {
	
	@Autowired
	private OrganizationDao organizationDao;

	@Override
	public List<Organization> getOrganizations(QueryFilter queryFilter) {
		return organizationDao.find(queryFilter);
	}

	@Override
	public Page<Organization> findOrganizations(Page<Organization> page, QueryFilter queryFilter) {
		return organizationDao.findPage(page, queryFilter);
	}

	@Override
	public Organization getOrganization(String id) {
		return organizationDao.get(id);
	}

	@Override
	public void saveOrganization(Organization organization) {
		organizationDao.save(organization);
	}

	@Override
	public void deleteOrganization(String id) {
		organizationDao.delete(id);
		
	}

	@Override
	public List<Organization> getAll() {
		List<Organization> list = organizationDao.getAll();
		return list;
	}

	@Override
	public List<Organization> getByParentIds(String id) {
		return organizationDao.findByParentIds(id);
	}

	@Override
	public Organization getTop() {
		return organizationDao.findTop();
	}

	/**
	 *  对接小修系统中的养护单位数据
	 */
	@Override
	public void dockAllOrgz() throws Exception {
		String urlString = ConfigUtils.getConfig("repairAddress");
		if (urlString.charAt(urlString.length()-1) != '/') {
			urlString +="/";
		}
		urlString += "gsj/organization.action?type=3";
		URL url = new URL(urlString);
		System.out.println("url:"+urlString);
		StringBuffer stbff = new StringBuffer();
		URLConnection urlConnection = url.openConnection();
		urlConnection.setConnectTimeout(30000);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));
		String line = null;
		while((line = bufferedReader.readLine()) != null){
			stbff.append(line);
		}
		System.out.println(stbff.toString());
		JsonBinder jsonBinder = new JsonBinder(Inclusion.ALWAYS);
		List<Organization> list = jsonBinder.fromJsonList(stbff.toString(), new TypeReference<List<Organization>>() {});
		System.out.println(list.size());
		Organization top = getTop();
		for (Organization organization : list) {
			Organization exist = organizationDao.findUniqueBy("dockingId", organization.getDockingId());
			if (exist == null) {
				organization.setParent(top);
				organization.setParentIds(top.getId()+",");
				organizationDao.save(organization);
			}else {
				continue;
			}
		}
	}

	@Override
	public Organization get(String id) {
		return organizationDao.get(id);
	}

	@Override
	public Organization getOrganizationByDockId(String dockId) {
		return organizationDao.findUniqueBy("dockingId", dockId);
	}

	@Override
	public List<Organization> findOrganizations(QueryFilter queryFilter) {
		return organizationDao.find(queryFilter, "createDate", false);
	}
	
	


}
